package com.humanresource.service.impl.clerkrecord;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.humanresource.dao.mapper.clerkrecord.InstMapper;
import com.humanresource.domain.po.clerkrecord.Inst;
import com.humanresource.domain.po.clerkrecord.InstExample;
import com.humanresource.service.clerkrecord.InstService;

@Service
public class InstServiceImpl implements InstService {

	@Autowired
	private InstMapper instMapper;

	@Override
	public Map<String, List<Inst>> instList() {
		Map<String, List<Inst>> instMap = new HashMap<String, List<Inst>>();
		InstExample instExample1 = new InstExample();
		InstExample.Criteria criteria1 = instExample1.createCriteria();
		criteria1.andInstpidEqualTo(0);
		List<Inst> inst1List = instMapper.selectByExample(instExample1);
		instMap.put("一级机构", inst1List);

		List<Inst> inst2List = new ArrayList<Inst>();
		for (Inst inst : inst1List) {
			InstExample instExample2 = new InstExample();
			InstExample.Criteria criteria2 = instExample2.createCriteria();
			criteria2.andInstpidEqualTo(inst.getInstid());
			List<Inst> instList = instMapper.selectByExample(instExample2);
			inst2List.addAll(instList);
		}
		instMap.put("二级机构", inst2List);

		List<Inst> inst3List = new ArrayList<Inst>();
		for (Inst inst : inst2List) {
			InstExample instExample3 = new InstExample();
			InstExample.Criteria criteria3 = instExample3.createCriteria();
			criteria3.andInstpidEqualTo(inst.getInstid());
			List<Inst> instList = instMapper.selectByExample(instExample3);
			inst3List.addAll(instList);
		}
		instMap.put("三级机构", inst3List);
		return instMap;
	}

	@Override
	public Inst findById(Integer instId) {
		return this.instMapper.selectByPrimaryKey(instId);
	}

	@Override
	public Inst findPById(Integer instId) {
		return this.instMapper.selectByPrimaryKey(this.findById(instId)
				.getInstpid());
	}

	@Override
	public Inst findByName(String instName) {
		InstExample instExample = new InstExample();
		InstExample.Criteria criteria = instExample.createCriteria();
		criteria.andInstnameEqualTo(instName);
		return this.instMapper.selectByExample(instExample).get(0);
	}
}
